package nl.sogyo.objectvisualisatie.GUI;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import nl.sogyo.objectvisualisatie.core.Model;
import nl.sogyo.objectvisualisatie.data.ObjectInfo;

/**
 * Show a list of all object that are active or
 * have been active in the JVM of the monitoring application
 * @author Ben
 *
 */

@SuppressWarnings("serial")
public class ObjectListView extends JPanel implements Observer, ListSelectionListener, MouseListener {
	
	private JList objectList;	   
    private DefaultListModel objectListModel;
    private Model model;
    	
	public ObjectListView()
	{
		super(new BorderLayout());
		objectListModel = new DefaultListModel();
        objectList = new JList(objectListModel);
        objectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        objectList.addListSelectionListener(this);
        objectList.setVisibleRowCount(10);
        objectList.addMouseListener(this);
        JScrollPane objectListScrollPane = new JScrollPane(objectList,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(objectListScrollPane, BorderLayout.CENTER);
		objectListModel.addElement("No objects created yet");
	}

	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
	}
	
	public void initalise(Model model) {
		this.model = model;
	}

	/**
	 * Update
	 */
	public void update() {
		if(model.getobjectsList().size() == 0)
		{
			objectListModel.removeAllElements();
		}
		
		objectListModel.removeAllElements();
		for(ObjectInfo objectInfo : model.getobjectsList()){
			objectListModel.addElement(objectInfo.getClassName());
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		for(ObjectInfo objectInfo : model.getobjectsList()){
			objectInfo.setSelected(false);		
		}
		ObjectInfo info = (ObjectInfo) model.getobjectsList().get(objectList.locationToIndex(e.getPoint()));
		info.setSelected(true);
		model.notifyObs();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub		
	}
}